package com.example.restaurantfinder.database;
 
import java.util.ArrayList;
import java.util.List;

import com.example.restaurantfinder.model.*;
 
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.webkit.WebChromeClient.CustomViewCallback;
 
public class DatabaseHandler extends SQLiteOpenHelper {
 
    // All Static variables
    // Database Version
    private static final int DATABASE_VERSION = 1;
 
    // Database Name
    private static final String DATABASE_NAME = "CuisineDatabase";
 
    //Cuisines table name
    private static final String TABLE_CUISINES = "cuisines";
 
    // Cuisines Table Columns names
    private static final String KEY_ID = "id";
    private static final String DESCRIPTION = "description";   
    
 
    public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
 
    // Creating Tables
    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_CUISINES_TABLE = "CREATE TABLE " + TABLE_CUISINES + "("
                + KEY_ID + " INTEGER PRIMARY KEY," + DESCRIPTION + " TEXT" + ")";
        db.execSQL(CREATE_CUISINES_TABLE);
    }
 
    // Upgrading database
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_CUISINES);
 
        // Create tables again
        onCreate(db);
    }
 
    /**
     * All CRUD(Create, Read, Update, Delete) Operations
     */
 
    // Adding new Cuisine
    public void addCuisine(Cuisine cuisine) {
        SQLiteDatabase db = this.getWritableDatabase();
 
        ContentValues values = new ContentValues();
        values.put(DESCRIPTION, cuisine.getDescription()); 
        
        // Inserting Row
        db.insert(TABLE_CUISINES, null, values);
        db.close(); // Closing database connection
    }
 
    // Getting single Cuisine
    public Cuisine getCuisine(int id) {
        SQLiteDatabase db = this.getReadableDatabase();
 
        Cursor cursor = db.query(TABLE_CUISINES, new String[] { KEY_ID,
                DESCRIPTION }, KEY_ID + "=?",
                new String[] { String.valueOf(id) }, null, null, null, null);
        if (cursor != null)
            cursor.moveToFirst();
 
        Cuisine cuisine = new Cuisine(Integer.parseInt(cursor.getString(0)),
                cursor.getString(1));
        // return Cuisine
        return cuisine;
    }
 
    // Getting All Cuisines
    public List<Cuisine> getAllObservations() {
        List<Cuisine> cuisines = new ArrayList<Cuisine>();
        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_CUISINES;
 
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
 
        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Cuisine cuisine = new Cuisine();
                cuisine.setId(Integer.parseInt(cursor.getString(0)));
                cuisine.setDescription(cursor.getString(1));
                
                // Adding cuisine to list
                cuisines.add(cuisine);
            } while (cursor.moveToNext());
        }
 
        // return cuisines list
        return cuisines;
    }
 
    // Updating single Cuisine
    public int updateObservation(Cuisine cuisine) {
        SQLiteDatabase db = this.getWritableDatabase();
 
        ContentValues values = new ContentValues();
        values.put(DESCRIPTION, cuisine.getDescription());
       
        // updating row
        return db.update(TABLE_CUISINES, values, KEY_ID + " = ?",
                new String[] { String.valueOf(cuisine.getId()) });
    }
 
    // Deleting single cuisine
    public void deleteCuisine(Cuisine cuisine) {
    	
    	SQLiteDatabase db = this.getWritableDatabase();
        db.delete(TABLE_CUISINES, KEY_ID + " = ?",
                new String[] { String.valueOf(cuisine.getId()) });
        db.close();
    }
 
    // Getting cuisines Count
    public int getCuisinesCount() {
        String countQuery = "SELECT  * FROM " + TABLE_CUISINES;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);
        cursor.close();
 
        // return count
        return cursor.getCount();
    }
 
}